import meshio


def write_gmsh(points_data:list[list[int]],elements_data:dict[int,list[int]],elements_sequence:dict[str,list[int]],file_name:str,target_path:str):
    cell_data = {}
    for k,v in elements_sequence.items():
        temp_list = []
        for i in v:
            temp_list.append(elements_data[i])
        cell_data[k] = temp_list

    suffix_location = file_name.rfind('.')
    file_name_before = ""
    if suffix_location != -1:
        file_name_before = file_name[:suffix_location]
    data = meshio.Mesh(points_data, cell_data)
    data.write(target_path+"//"+file_name_before+".msh",file_format="gmsh")
    return "success"


def write_gmsh_from_vtk(vtk_path,target_path:str):
    mesh = meshio.read(vtk_path)
    mesh_without_field = meshio.Mesh(
        points=mesh.points,
        cells=mesh.cells,
        # cell_data=mesh.cell_data,
        # point_data=mesh.point_data,
    )
    meshio.write(target_path, mesh_without_field, file_format="gmsh22", binary=False)


# if __name__ == '__main__':
    # a = [[0,0,0],[0,1,0],[1,0,0],[1,0,0]]
    # b = {1:[0,1,2],2:[1,2,3],3:[0,1,2,3]}
    # c = {"triangle":[1,2],"quad":[3]}
    # write_gmsh(a,b,c,"AAAA.inp","D:\\test\\test_file")

    # test = meshio.read("D:\\test\\INP_FILE\\new_export_hot.inp")
    # test.write("test1.gmsh",file_format="gmsh")
